import axios from 'axios';

import nProgress  from 'nprogress';

import "nprogress/nprogress.css"

import store from '@/store/index'

const request = axios.create({
    baseURL:process.env.VUE_APP_API,
    timeout: 10000
});

//配置当前axios的请求拦截器
request.interceptors.request.use(
    
    (config) => {
        //当请求发送的时候，激活进度条
        nProgress.start()
        let userTempId = store.state.detail.userTempId;
        config.headers.userTempId = userTempId 

        let token = store.state.detail.token;
        config.headers.token = token 
        return config
    },
    (err) => {
        return Promise.reject(err.message);
    }
);

//配置当前axios的响应拦截器
request.interceptors.response.use(
    (response) => {
        nProgress.done();
        //判断请求的code是否为200
        if(response.data.code === 200 ){
            return response.data.data
        }else{
            return Promise.reject({message:response.data.message})
        }
        
    },
    (err) => {
        return Promise.reject(err.message)
    }
)


export default request